#!/usr/bin/env node

/**
 * 检查远程equipment_base表结构
 */

const mysql = require('mysql2/promise');

const remoteDbConfig = {
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    charset: 'utf8mb4'
};

async function checkTableStructure() {
    let connection;

    try {
        console.log('🔌 连接远程数据库...');
        connection = await mysql.createConnection(remoteDbConfig);
        console.log('✅ 远程数据库连接成功');

        // 检查表结构
        console.log('🔍 检查equipment_base表结构...');
        const [columns] = await connection.execute(`
            SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_COMMENT
            FROM information_schema.COLUMNS 
            WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'equipment_base'
            ORDER BY ORDINAL_POSITION
        `, [remoteDbConfig.database]);

        console.log('📋 equipment_base表结构:');
        columns.forEach(col => {
            console.log(`   ${col.COLUMN_NAME}: ${col.DATA_TYPE} ${col.IS_NULLABLE === 'NO' ? 'NOT NULL' : 'NULL'} ${col.COLUMN_DEFAULT ? `DEFAULT ${col.COLUMN_DEFAULT}` : ''} ${col.COLUMN_COMMENT ? `COMMENT '${col.COLUMN_COMMENT}'` : ''}`);
        });

        // 检查现有数据
        console.log('\n📊 检查现有数据...');
        const [rows] = await connection.execute('SELECT COUNT(*) as count FROM equipment_base');
        console.log(`   总记录数: ${rows[0].count}`);

        if (rows[0].count > 0) {
            const [sampleRows] = await connection.execute('SELECT * FROM equipment_base LIMIT 3');
            console.log('\n📋 前3条数据示例:');
            sampleRows.forEach((row, index) => {
                console.log(`   记录${index + 1}:`, row);
            });
        }

    } catch (error) {
        console.error('❌ 检查失败:', error.message);
    } finally {
        if (connection) {
            await connection.end();
            console.log('\n🔌 数据库连接已关闭');
        }
    }
}

checkTableStructure().catch(console.error);
